﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using SuraQR.Bean;
using System.Collections;

namespace SuraQR.Controller
{
    class XmlToMenuService
    {
         private XmlDataDocument xmldoc; 
         private XmlNodeList xmlnode ;
         private ArrayList menuList;

         public ArrayList MenuList
         {
             get { return menuList; }
             private set { menuList = value; }
         }
         
        public XmlToMenuService(){
            xmldoc = new XmlDataDocument();
            menuList = new ArrayList();
        }

        public void read()
        {
          
            Debug.WriteLine("Read Menu Config XML");
            FileStream fs = null;
            try
            {
                 fs = new FileStream("SuraTaxMaster.xml", FileMode.Open, FileAccess.Read);
            }
            catch (FileNotFoundException e)
            {
                Debug.WriteLine("File not found : create" + e);
                System.IO.File.WriteAllText(@"SuraTaxMaster.xml", SuraQR.Properties.Resources.SuraTaxMaster);
                 fs = new FileStream("SuraTaxMaster.xml", FileMode.Open, FileAccess.Read);
            }
            try
            {

            xmldoc.Load(fs);
            xmlnode = xmldoc.GetElementsByTagName("TaxCal");
            int childs = xmlnode[0].ChildNodes.Count;
            for (int i = 0; i < childs; i++)
            {
                XmlNodeList node = xmlnode[0].ChildNodes.Item(i).ChildNodes;
                Debug.WriteLine("L : " + i);
                this.parse(node);
            }

            Debug.WriteLine("finish read XML Size : " + menuList.Count);
            fs.Close();

            }catch(Exception e){
                MessageBox.Show("Error Format in XML \n" + e.ToString());
                MenuList.Clear();
            }
        }


        private void parse( XmlNodeList node){
            MenuOption m = new MenuOption();

            int c = node.Count;
            for(int i = 0 ; i < c ; i++){
                XmlNode n = node.Item(i);
//                Type	adValoremTax	quantityTax1	quantityTax2	overloadegree	
//                overloadegreeTax	TellPriceFlage	GOVPriceFlage	condition	param	Option

                if( n.Name == "Type"){
                    m.Type = n.InnerText;
                }
                else if (n.Name == "adValoremTax")
                {
                    m.AdValoremTax = Convert.ToDouble( n.InnerText );
                }
                else if (n.Name == "quantityTax1")
                {
                    m.QuantityTax1 = Convert.ToDouble(n.InnerText);
                }
                else if (n.Name == "quantityTax2")
                {
                    m.QuantityTax2 = Convert.ToDouble(n.InnerText);
                }
                else if (n.Name == "overloadegree")
                {
                    m.Overloadegree = Convert.ToDouble(n.InnerText);
                }
                else if (n.Name == "overloadegreeTax")
                {
                    m.OverloadegreeTax = Convert.ToDouble(n.InnerText);
                }
                else if (n.Name == "TellPriceFlage")
                {
                    m.TellPriceFlage1 = n.InnerText;
                }
                else if (n.Name == "GOVPriceFlage")
                {
                    m.GOVPriceFlage1 = n.InnerText;
                }
                else if (n.Name == "condition")
                {
                    m.Condition = n.InnerText;
                }
                else if (n.Name == "param")
                {
                    m.Param = n.InnerText;
                }
                else if (n.Name == "Option")
                {
                    m.Option1 = n.InnerText;
                }

               
            }
            MenuList.Add(m);
        }
    }
}
